博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[MST] Build Forms with React to Edit mobx-state-tree Models
阅读量:6939 次
发布时间:2019-06-27

本文共 2606 字,大约阅读时间需要 8 分钟。

We will expand our UI, and give the user the possibility to edit his wishlist. We will use the earlier defined actions. We also will use model clones only to modify data temporarily, and after approving the changes, apply them back to the original model.

In this lesson you will learn:

  • How to call model actions from a component
  • Using clone to create a full, deep clone of any model instance
  • Using applySnapshot to update the state of a model instance given a snapshot.

 

The whole point for building a editing form component is that:

  1. avoid two ways data flow, means that you change the data inside the form, without saving but the data was mutated already. To solve this problem, we will use 'clone' from 'mobx-state-tree'.

  2. When save the data, we can use 'getSnapshot' and 'applySnapshot' from the lib.

import React, {Component} from "react"import {observer} from "mobx-react";import {clone, getSnapshot, applySnapshot} from 'mobx-state-tree';import WishListItemEdit from "./WishListItemEdit"class WishListItemView extends Component {    constructor() {        super();        this.state = {isEditing: false}    }    render() {        const {item} = this.props;        return this.state.isEditing ?            this.renderEditable() :            this.renderItemView(item);    }    renderEditable() {        return (            
  • ); } renderItemView(item) { return (
  • {item.image && }

    {item.name}

    {item.price}
  • ); } onSaveEdit = () => { applySnapshot(this.props.item, getSnapshot(this.state.clone)) this.setState({ isEditing: false, clone: null }) }; onCancelEdit = () => { this.setState({isEditing: false}) }; onToggleEdit = () => { this.setState({ isEditing: true, clone: clone(this.props.item) }) }}export default observer(WishListItemView)

     

    Here we have to use the methods provide from the lib, because the 'this.props.item' is a mobx state model object:

    {$mobx: ObservableObjectAdministration, toString: ƒ, …}

     

    The good part of this approach is that, the model related data can be handled by the mobx-state-tree lib. It can helps to udpate the model efficiently.

    The down side of the approach is that,  you needs to keep at least tow parts of state, one is mobx-state-tree, another one is the component state, for example, in the code, 'isEiditing' & 'clone'. 

     

    转载地址:http://uefnl.baihongyu.com/

    你可能感兴趣的文章
    由《旧制度与大革命》提取的5个感触
    查看>>
    sqlserver 分页
    查看>>
    php通过system()调用Linux命令问题
    查看>>
    swift 警告框 - 自定义按钮颜色,图片
    查看>>
    提高搜索引擎结果页面排名的各种技术
    查看>>
    刷题常用的STL容器总结
    查看>>
    创建一个支持ES6的Nodejs项目
    查看>>
    sqlserver 行转列、字符串行转列、自动生产行转列脚本
    查看>>
    仿微信表情输入
    查看>>
    慎用dictionaryWithObjectsAndKeys方法
    查看>>
    兼容FF IE的回车事件
    查看>>
    冒泡排序,快速排序, 二叉树,一致性哈希
    查看>>
    sdut 1451 括号东东 (dp或模拟)
    查看>>
    POJ1002 487-3279
    查看>>
    Visual Studio 2012+jQuery-1.7.1
    查看>>
    Appium 在 Android UI 测试中的应用
    查看>>
    登录界面 动画背景效果
    查看>>
    DEV 第三方控件报表分类汇总
    查看>>
    Linux c 学习第一天
    查看>>
    ios 安卓
    查看>>